package run;

import entity.config.ConfigInfo;
import entity.table.TableDto;
import entity.table.TableFileDto;
import entity.table.TableInfoDto;
import service.clazz.ClazzService;
import service.config.ConfigService;
import utils.common.JdbcUtil;
import utils.io.FileUtil;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.io.StringWriter;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Properties;

/**
 * 构造器：需要生成的文件
 */
public class Constructor {

    /*public static void main(String args[]) throws IOException {
        ClazzService clazzService = new ClazzService();
        List<TableDto> list = JdbcUtil.DBManager("SELECT table_name, column_name, data_type, column_comment, column_key FROM information_schema. COLUMNS WHERE table_name in ( 't_user' )");

        TableInfoDto tableInfoDto = new TableInfoDto();

        tableInfoDto.setEntityPackaging("autocode");
        tableInfoDto.setServicePackaging("autocode");
        tableInfoDto.setServiceImplPackaging("autocode");
        tableInfoDto.setDaoPackaging("autocode");
        tableInfoDto.setMapperXmlPackaging("autocode");

        tableInfoDto.setTableName("t_user");
        tableInfoDto.setRemovePrefix("t_");

        // 输出
        List<String> clazzList = clazzService.generateClazz(list, tableInfoDto);
        for (String str : clazzList) {
            System.out.println(str);
            File file = new File("C:\\Users\\LX\\Desktop\\out\\xxx.java");
            FileUtil.writeFile(file, str, "UTF-8");
        }
    }*/

    public static void main(String args[]) throws Exception {
        // 读取配置文件
        String url = "C:/z/kalimdor/code/src/main/resources/kalimdor/code-generation.properties";

        ClazzService clazzService = new ClazzService();
        ConfigInfo configInfo = new ConfigService(url).readConfig();

        List<TableFileDto> codeList = clazzService.parsingTable(configInfo);
        for (TableFileDto fileDto : codeList) {
            System.out.println(fileDto.toString());
            File file = new File("C:\\Users\\LX\\Desktop\\out\\"+fileDto.getFileName());
            FileUtil.writeFile(file, fileDto.getContext(), "UTF-8");
        }
    }
}
